package com.keyinong.restaurant;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
//import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.restaurant.common.BaseActivity;
import com.keyinong.restaurant.tools.HttpTools;
import com.keyinong.restaurant.view.MainActivity;
import com.keyinong.restaurant.view.OrderDetailActivity;
import com.loopj.android.http.JsonHttpResponseHandler;

public class ResultActivity extends BaseActivity{
	WebView vb;
	View iscatview = null;
	//	TextView res;
	//	ImageView res_pic;
	//	ImageButton copy;
	//	ImageButton share;
	//	ImageButton browser;
	HttpTools httpTools=null;
	private Button btn_queren;
	private ImageButton ib_fanhui5;
	String orderid;
	String paymodeid;
	String token;
	String state;
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		WebView vb=(WebView)findViewById(R.id.webView1);
		vb.getSettings().setJavaScriptEnabled(true);  
		vb.getSettings().setDomStorageEnabled(true);  
		SharedPreferences sp = getSharedPreferences("spReg",
				Context.MODE_PRIVATE);
		token = sp.getString("token", "");
		String url= getIntent().getExtras().getString("scan_res");
		String[] strarray1 = url.split("/");
		orderid = strarray1[strarray1.length - 1].toString();
//		final String orderid = strarray1[strarray1.length - 3].toString();
//		orderDetail();
		
		vb.loadUrl(url);
		//		initView();
		btn_queren=(Button)findViewById(R.id.btn_queren);
		btn_queren.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				confirmOrder();
//				if(paymodeid.equals("4")){
//					paymodedialog();
//				}else {
//					queren();
//				}
//				
			}
		});
		ib_fanhui5=(ImageButton)findViewById(R.id.ib_fanhui5);
		ib_fanhui5.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	private void confirmOrder(){
		if (httpTools == null) {
			httpTools = new HttpTools(ResultActivity.this);
		}
		httpTools.finishOrder(token, orderid,
				new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					boolean code = response.getBoolean("code");
					String msg = response.getString("msg");
//					Log.i("zq", "adapter---"+response.toString());
					
					
					if (code == true) {
						btn_queren.setText("订单已完成");
						finish();
					} 
						Toast.makeText(ResultActivity.this, msg, Toast.LENGTH_SHORT).show();
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			@Override
			public void onFailure(int statusCode, Header[] headers,
				String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
			Toast.makeText(ResultActivity.this, "确认收货失败",
					Toast.LENGTH_SHORT).show();
			}

		});
	}
	
	/**确认取餐*/
//	private void queren(){
//		if (httpTools == null) {
//			httpTools = new HttpTools(getApplicationContext());
//		}
//		httpTools.yiwancheng(token, orderid,"3",paymodeid,
//				new JsonHttpResponseHandler() {
//
//			@Override
//			public void onSuccess(int statusCode, Header[] headers,
//					JSONObject response) {
//				super.onSuccess(statusCode, headers, response);
//				Log.e("lh", "queren_response=="+response.toString());
//				try {
//					boolean code = response.getBoolean("code");
//					String msg = response.getString("msg");
//					if (code == true) {
//						Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
//						Intent intent = new Intent();
//						intent.setClass(getApplicationContext(), MainActivity.class);
//						startActivity(intent);
////						finish();
//						//										view.setBackgroundResource(R.drawable.shouchang1);
//					} else {
//						Toast.makeText(getApplicationContext(), "订单已完成！", Toast.LENGTH_SHORT).show();
//					}
//				} catch (JSONException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//			@Override
//	public void onFailure(int statusCode, Header[] headers,
//		String responseString, Throwable throwable) {
//		super.onFailure(statusCode, headers, responseString, throwable);
//		Toast.makeText(getApplicationContext(), "确认取货失败",
//				Toast.LENGTH_SHORT).show();
//		}
//
//		});
//	}
	
	/**订单详情*/
//	private void orderDetail() {
//		if (httpTools == null) {
//			httpTools = new HttpTools(ResultActivity.this);
//		}
//		httpTools.detail(token, orderid, new JsonOrderDetail());
//	}

	private class JsonOrderDetail extends JsonHttpResponseHandler {
		@Override
		public void onSuccess(int statusCode, Header[] headers,
				JSONObject response) {
			super.onSuccess(statusCode, headers, response);
			Log.e("lh", "orderdetail---" + response.toString());
			try {
				boolean code = response.getBoolean("code");
				if (code == true) {
					JSONObject object = response.getJSONObject("data");
					JSONArray array = object.getJSONArray("details");
					paymodeid = object.getString("paymodeid");
					state = object.getString("state");
					for (int i = 0; i < array.length(); i++) {
						JSONObject object2 = array.getJSONObject(i);
						String id = object2.getString("id");
						String name = object2.getString("name");
						String amount = object2.getString("amount");
						String price = object2.getString("price");
						String content = object2.getString("content");
						//							gonds.add(content+" - ");
					}
					if(state.equals("3")){
//						tv_cancle.setVisibility(View.GONE);
						btn_queren.setText("订单已完成");
						btn_queren.setEnabled(false);
					}else if(state.equals("1")){
//						tv_cancle.setVisibility(View.VISIBLE);
						btn_queren.setText("确认取餐");
					}else{
//						tv_cancle.setVisibility(View.GONE);
						btn_queren.setText("确认取餐");
					}
//					if(note.equals("")||note.equals("null")){
//						tv_content.setText("无");
//						note="无";
//					}else{
//						tv_content.setText(note);
//					}
//					if(recmode.equals("1")){
//						tv_fee.setVisibility(View.INVISIBLE);
//					}else if (recmode.equals("2")) {
//						tv_fee.setVisibility(View.VISIBLE);
//						tv_fee.setText("(含运费￥"+fee+")");
//					}
//					tv_shuliang.setText(""+shuliang+"份");
//					tv_zongjia.setText(""+heji+"元");

//					caidanAdapter = new CaidanAdapter(context, detaillist);
//					lv.setAdapter(caidanAdapter);
//					lv.setDivider(null);
//					Utility.setListViewHeightBasedOnChildren(lv);
					//							Log.i("zq", "list"+array.length());
				} else {
					String msg = response.getString("msg");
					Toast.makeText(ResultActivity.this, msg, Toast.LENGTH_SHORT).show();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		@Override
		public void onFailure(int statusCode, Header[] headers,
				String responseString, Throwable throwable) {
			// TODO Auto-generated method stub
			super.onFailure(statusCode, headers, responseString, throwable);
			Log.e("lh", "onFailure---" + responseString);
			Toast.makeText(ResultActivity.this, "获取商品信息失败", 
					Toast.LENGTH_SHORT).show();
		}

	}
	
	/**支付方式弹窗*/
	private void paymodedialog() {

		final Dialog dialog = new Dialog(ResultActivity.this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

		iscatview = LayoutInflater.from(ResultActivity.this).inflate(
				R.layout.dialog_paymode, null);
		//			TextView tital = (TextView) iscatview.findViewById(R.id.tital);
		//			TextView tv_phone = (TextView) iscatview.findViewById(R.id.tv_phone);
		TextView tv_shuaka = (TextView) iscatview.findViewById(R.id.tv_shuaka);
		TextView tv_zhifubao = (TextView) iscatview.findViewById(R.id.tv_zhifubao);
		TextView tv_weixin = (TextView) iscatview.findViewById(R.id.tv_weixin);
		TextView tv_xianjin = (TextView) iscatview.findViewById(R.id.tv_xianjin);
		dialog.setContentView(iscatview);
		dialog.show();
		tv_shuaka.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (httpTools == null) {
					httpTools = new HttpTools(getApplicationContext());
				}
				httpTools.yiwancheng(token, orderid,"3","5",
						new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							boolean code = response.getBoolean("code");
							String msg = response.getString("msg");
							if (code == true) {
								Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
								Intent intent = new Intent();
								intent.setClass(getApplicationContext(), MainActivity.class);
								startActivity(intent);
//								finish();
								//										view.setBackgroundResource(R.drawable.shouchang1);
							} else {
								Toast.makeText(getApplicationContext(), "订单已完成！", Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});
				dialog.dismiss();
			}
		});
		tv_zhifubao.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (httpTools == null) {
					httpTools = new HttpTools(getApplicationContext());
				}
				httpTools.yiwancheng(token, orderid,"3","6",
						new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							boolean code = response.getBoolean("code");
							String msg = response.getString("msg");
							if (code == true) {
								Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
								Intent intent = new Intent();
								intent.setClass(getApplicationContext(), MainActivity.class);
								startActivity(intent);
//								finish();
								//										view.setBackgroundResource(R.drawable.shouchang1);
							} else {
								Toast.makeText(getApplicationContext(), "订单已完成！", Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});
				dialog.dismiss();
			}
		});
		tv_weixin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (httpTools == null) {
					httpTools = new HttpTools(getApplicationContext());
				}
				httpTools.yiwancheng(token, orderid,"3","7",
						new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							boolean code = response.getBoolean("code");
							String msg = response.getString("msg");
							if (code == true) {
								Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
								Intent intent = new Intent();
								intent.setClass(getApplicationContext(), MainActivity.class);
								startActivity(intent);
//								finish();
								//										view.setBackgroundResource(R.drawable.shouchang1);
							} else {
								Toast.makeText(getApplicationContext(), "订单已完成！", Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});
				dialog.dismiss();
			}
		});
		tv_xianjin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (httpTools == null) {
					httpTools = new HttpTools(getApplicationContext());
				}
				httpTools.yiwancheng(token, orderid,"3","8",
						new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							boolean code = response.getBoolean("code");
							String msg = response.getString("msg");
							if (code == true) {
								Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
								Intent intent = new Intent();
								intent.setClass(getApplicationContext(), MainActivity.class);
								startActivity(intent);
//								finish();
								//										view.setBackgroundResource(R.drawable.shouchang1);
							} else {
								Toast.makeText(getApplicationContext(), "订单已完成！", Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});
				dialog.dismiss();
			}
		});
	}
	
	
	public void initView(){

		//		String scan_res=getIntent().getExtras().getString("scan_res");
		//		String scan_pic=getIntent().getExtras().getString("scan_pic");
		//		
		//		res=(TextView)findViewById(R.id.res);
		//		res_pic=(ImageView)findViewById(R.id.res_pic);
		//		copy=(ImageButton)findViewById(R.id.copy);
		//		share=(ImageButton)findViewById(R.id.share);
		//		browser=(ImageButton)findViewById(R.id.browser);
		//		
		//		Bitmap bitmap = getLoacalBitmap(scan_pic); 
		//		res.setText(scan_res);
		//		res_pic.setImageBitmap(bitmap);
		//		
		//		copy.setOnClickListener(this);
		//		share.setOnClickListener(this);
		//		browser.setOnClickListener(this);
	}

	/**
	 * 加载本地图片
	 * @param url
	 * @return
	 */
	public static Bitmap getLoacalBitmap(String url) {
		try {
			FileInputStream fis = new FileInputStream(url);
			return BitmapFactory.decodeStream(fis);  ///把流转化为Bitmap图片        
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}



	//	@Override
	//	public void onClick(View v) {
	//	if(v==copy){
	//		App.copy(res.getText().toString(), ResultActivity.this);
	//	}
	//	if(v==share){
	//		Intent intent = new Intent(Intent.ACTION_SEND); // 启动分享发�?到属�??		
	//		intent.setType("text/plain"); // 分享发�?到数据类�??		//
	//		intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); // 分享的主�??		
	//		intent.putExtra(Intent.EXTRA_TEXT, res.getText().toString()); // 分享的内�??		
	//		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 允许intent启动新的activity
	//		startActivity(Intent.createChooser(intent, "分享")); // //目标应用选择对话框的标题
	//	}
	//	if(v==browser){
	//		try {
	//			Intent intent= new Intent();        
	//		    intent.setAction("android.intent.action.VIEW");    
	//		    Uri content_url = Uri.parse(res.getText().toString());   
	//		    intent.setData(content_url);  
	//		    startActivity(intent);
	//		} catch (Exception e) {
	//			showToast("无效网址");
	//		}
	//
	//	}
	//	}

}
